package com.cqupt.qualityevaluation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqupt.qualityevaluation.controller.VO.schoolVO;
import com.cqupt.qualityevaluation.controller.VO.schooluserVO;
import com.cqupt.qualityevaluation.dao.expertMapper;
import com.cqupt.qualityevaluation.dao.schooluserMapper;
import com.cqupt.qualityevaluation.pojo.User;
import com.cqupt.qualityevaluation.pojo.expert;
import com.cqupt.qualityevaluation.pojo.school;
import com.cqupt.qualityevaluation.pojo.schooluser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/schooluser")
public class SchoolUserController {
    @Autowired
    private schooluserMapper schooluserMapper;
    @GetMapping("/findAll")
    public List<schooluser> findAll(){
        return schooluserMapper.selectList(null);
    }
    //分页功能
    @GetMapping("/page")
    public schooluserVO queryList(Integer current, Integer size){
        schooluserVO schooluserVO=new schooluserVO();
        IPage<schooluser> page=new Page<>(current,size);
        schooluserMapper.selectPage(page,null);
        schooluserVO.setCurrent(current);
        schooluserVO.setSize(size);
        schooluserVO.setTotal(page.getTotal());
        schooluserVO.setSchooluserList(page.getRecords());
        return schooluserVO;
    }
    @PostMapping("/save")
    //通过RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的),将JSON对象转换为java对象
    public Object save(@RequestBody schooluser s){
        Map map=new HashMap<>();
        int result=0;
        try {
            result=schooluserMapper.insert(s);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code","fail");
            map.put("msg","该角色名已存在");
            return map;
        }
        if (result==1){
            map.put("code","success");
            map.put("msg","添加成功");
        }
        else {
            map.put("code", "fail");
            map.put("msg","添加失败");
        }
        return map;
    }
    @DeleteMapping("/delete/{username}")
    public String delete(@PathVariable("username") String username){
        QueryWrapper<schooluser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        schooluserMapper.delete(queryWrapper);
        return "success";
    }
    @PutMapping("/update")
    public Object update(@RequestBody schooluser s){
        Map map=new HashMap<>();
        int result=0;
        UpdateWrapper<schooluser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",s.getId());
        schooluser schooluser = new schooluser();
        schooluser.setUsername(s.getUsername());
        schooluser.setPassword(s.getPassword());
        schooluser.setSchoolname(s.getSchoolname());
        try {
            result=schooluserMapper.update(schooluser,updateWrapper);
        }catch (Exception e){
            map.put("code","fail");
            map.put("msg","该角色名已存在");
            return map;
        }
        if (result==1){
            map.put("code","success");
            map.put("msg","更新成功");
        }
        else {
            map.put("code", "fail");
            map.put("msg","添加失败");
        }
        return map;
    }

    @GetMapping("/selectone/1/{id}")
    public schooluser findByschooluserId(@PathVariable("id") String id){
        QueryWrapper<schooluser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return schooluserMapper.selectOne(queryWrapper);
    }
    @GetMapping("/selectone/2/{username}")
    public schooluser selectByschooluserUsername(@PathVariable("username") String username){
        QueryWrapper<schooluser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return schooluserMapper.selectOne(queryWrapper);
    }
    @GetMapping("/selectone/3/{schoolname}")
    public schooluser selectByschooluserSchoolname(@PathVariable("schoolname") String username){
        QueryWrapper<schooluser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schoolname",username);
        return schooluserMapper.selectOne(queryWrapper);
    }
    @GetMapping("/search/1/{username}")
    public List<schooluser> findByschooluserUsername(@PathVariable("username") String username){
        QueryWrapper<schooluser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username",username);
        return schooluserMapper.selectList(queryWrapper);
    }
    @GetMapping("/search/2/{password}")
    public List<schooluser> findByschooluserPassword(@PathVariable("password") String password){
        QueryWrapper<schooluser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("password",password);
        return schooluserMapper.selectList(queryWrapper);
    }
    @GetMapping("/search/3/{schoolname}")
        public List<schooluser> findByschooluserSchoolName(@PathVariable("schoolname") String schoolname){
        QueryWrapper<schooluser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("schoolname",schoolname);
        return schooluserMapper.selectList(queryWrapper);
    }
}
